﻿using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using AnyOffice.Model;
using FredCK.FCKeditorV2;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.work_plan.manage
{
    public partial class _new :  ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件添加", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";
            try
            {
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 4;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            PlanWorkDao dao2 = new PlanWorkDao();
            PlanWorkData data2 = new PlanWorkData();
            data2.AttachmentId = attachmentId;
            data2.Name = this.txtName.Value;
            data2.Content = this.txtContent.Value;
            if (this.txtBeginDate.Value != "")
            {
                data2.BeginDate = DateTime.Parse(this.txtBeginDate.Value);
            }
            if (this.txtEndDate.Value != "")
            {
                data2.EndDate = DateTime.Parse(this.txtEndDate.Value);
            }
            data2.TypeId = Convert.ToInt32(this.ddlTypeId.SelectedValue);
            data2.ToId = this.ToId.Value;
            data2.Participator = this.ParticipatorId.Value;
            data2.Manager = this.ManagerId.Value;
            data2.AttachmentComment = this.txtAttachmentComment.Value;
            data2.Remark = this.txtRemark.Value;
            data2.Creator = base.User.Identity.Name;
            if (this.rdoStatus0.Checked)
            {
                data2.Status = 0;
            }
            if (this.rdoStatus1.Checked)
            {
                data2.Status = 1;
            }
            if (this.rdoStatus2.Checked)
            {
                data2.Status = 2;
            }
            data2.CompletedPercent = Convert.ToInt32(this.sltCompletedPercent.Value);
            dao2.Insert(base.Conn, data2);
            if (this.ckbSms.Checked || this.ckbMobile.Checked)
            {
                string str3 = this.ParticipatorId.Value;
                if (this.ManagerId.Value != string.Empty)
                {
                    str3 = str3 + this.ManagerId.Value;
                }
                if (str3.EndsWith(","))
                {
                    str3 = str3.Substring(0, str3.Length - 1);
                }
                UserCache cache = new UserCache();
                SmsData data3 = new SmsData();
                SmsDao dao3 = new SmsDao();
                data3.Type = 12;
                data3.FromId = base.User.Identity.Name;
                data3.ToDeptList = this.ToId.Value;
                data3.ToIdList = str3;
                data3.Content = cache.UserName + "发布新的项目，请注意查看。";
                data3.RemindFlag = true;
                data3.SendTime = DateTime.Now;
                dao3.BizSend(base.Conn, data3, this.ckbSms.Checked, this.ckbMobile.Checked);
            }
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("项目新建成功！", "index1.aspx");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new PlanTypeDao().SelectAll(base.Conn);
                this.ddlTypeId.DataValueField = "TypeId";
                this.ddlTypeId.DataTextField = "TypeName";
                this.ddlTypeId.DataSource = table;
                this.ddlTypeId.DataBind();
                this.ddlTypeId.Items.Insert(0, new ListItem("", ""));
                this.txtBeginDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.ViewState["FileNums"] = 0;
                this.ViewState["FileList"] = new ArrayList();
                this.ViewState["UniqueTempPath"] = "";
                this.ViewState["TotalSize"] = 0;
                this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.WorkPlan);
            }
        }
 

    }
}
